<?php

namespace App\Http\Controllers\Api;

use App\Models\Trend;
use App\Repositories\Api\LikeRepository;
use App\Repositories\Api\ReportRepository;
use App\Repositories\Api\TrendCommentRepository;
use App\Repositories\Api\TrendRepository;
use App\Repositories\Api\MemberRepository;

use Cache;

class TrendController extends MiniAppController
{
    private $memberRep = NULL,
        $trendRep = NULL,
        $commentRep = NULL;

    public function __construct(
        MemberRepository       $memberRep,
        TrendRepository        $trendRep,
        TrendCommentRepository $commentRep
    )
    {
        parent::__construct();
        $this->memberRep = $memberRep;
        $this->trendRep = $trendRep;
        $this->commentRep = $commentRep;
    }

    public function index()
    {
        $params = request()->all();
        $res = $this->trendRep->getLists($params, $this->pageSize, $this->pageNum);
        return retSuc($res);
    }

    public function detail()
    {
        $trend_id = intval(request()->get("id"));
        if (empty($trend_id)) {
            return retFail("参数错误");
        }
        $res = $this->trendRep->getOne($trend_id);

        Trend::query()->where(['id' => $trend_id])->increment("pv");

        //点赞会员
        $memberList = (new LikeRepository())->getLists(['object_id' => $trend_id, 'object_type' => "trend"], 6, 1);
        return retSuc(['trend' => $res, "starMember" => $memberList]);
    }

    public function add()
    {
        $params = request()->all();
        return $this->trendRep->addTrend($params);
    }

    public function talk_type()
    {
        $params = request()->all();
        $params['status'] = 1;
        $res = $this->trendRep->getTalkTypeList($params);

        return retSuc($res ?? []);
    }

    //点赞 动态/评论
    public function like()
    {
        $object_id = request()->get("id", 0, "intval");
        $type = intval(request()->get("type")); //1：评论点赞
        return $this->trendRep->like($object_id, $type ? 'trend_comment' : 'trend', UID);
    }

    /**
     * 点赞-人
     */
    public function like_member()
    {
        $object_id = request()->get("id");
        $object_type = request()->get("object_type");
        $res = (new LikeRepository())->getLists(['object_id' => intval($object_id), 'object_type' => $object_type], $this->pageSize, $this->pageNum);
        return retSuc($res);
    }

    //评论
    public function add_comment()
    {
        $params = request()->all();
        return $this->commentRep->addComment($params);
    }

    //评论-列表
    public function comment_list()
    {
        $params = request()->all();
        $params['status'] = 1;
        $res = $this->commentRep->getLists($params, $this->pageSize, $this->pageNum);
        return retSuc($res);
    }

    public function report()
    {
        $params = request()->all();
        if (empty(intval($params['object_id'])) || empty($params['object_type']))
            return retFail("参数错误");
        if (empty(intval($params['type']))) {
            return retFail("举报内容必填");
        }

        return (new ReportRepository())->addReport($params);
    }
}
